#-*- codeing = utf-8 -*-
#@Time:2022/9/148:36
#@Author :计科1813-14-张玉婷
#@File:dict.py
#@Software:PyCharm

'''
dic1={"name": "小明", "age": 20}
print(dic1["name"])
#访问了不存在的键
print(dic1["gender"])
print(dic1.get("gender"))  #None
print(dic1.get("age", 18))  #20
print(dic1.get("height", 170))  #170没找到可以设定默认值
'''
'''
#增
dic1 = {"name": "小明", "age": 20}
newID = input("请输入新的学号：")
dic1["id"] = newID
print(dic1["id"])
'''

#删
#[del]
'''
dic1 = {"name": "小明", "age": 20}
print("删除前：%s" % dic1["name"])

del dic1["name"]

print("删除后：%s" % dic1["name"])  #删除了指定键值对后，再次访问会报错
'''
'''
dic1 = {"name": "小明", "age": 20}
print("删除前：%s" % dic1)
del dic1

#print("删除后：%s" % dic1)  #删除字典后在访问，报错
'''
'''
#[clear]   清空
dic1 = {"name": "小明", "age": 20}
print("清空前：%s" % dic1)
dic1.clear()
print("清空后：%s" % dic1)
#清空前：{'name': '小明', 'age': 20}
#清空后：{}
'''
'''
#改
dic1 = {"name": "小明", "age": 20}
dic1["age"] = 18
print(dic1["age"])
'''

#查
#dic1 = {"id": 1, "name": "小明", "age": 20}
#print(dic1.keys())   #得到所有的键（列表）

#print(dic1.values())   #得到所有的值（列表）

#print(dic1.items())   #得到所有的项（列表），每个键值对是一个元组
'''
#遍历所有键
for key in dic1.keys():
    print(key)

#遍历所有值
for value in dic1.values():
    print(value)
'''
'''
#遍历所有键值对
for key, value in dic1.items():
    print("key=%s,value=%s" % (key, value))
#使用枚举函数，同时拿到列表中的下标和元素内容
mylist = ["a", "b", "c", "d"]
for i, x in enumerate(mylist):
    print(i, x)
'''
dic1 = {"name": "小明", "age": 20}
for key in dic1.keys():  #遍历所有键
    print(key)
for value in dic1.values():  #遍历所有值
    print(value)
for key, value in dic1.items():  #遍历所有键值对
    print("key={0},value={1}".format(key, value))